/**
 * Wihidum - Efficient Broker Hierarchy for Publish Subscribe Middleware
 *
 * License      :   Apache License 2.0
 * Home Page    :   http://code.google.com/p/wihidum/
 * Mailing List :   wihidum@googlegroups.com
 */

package lk.mrt.cse.wihidum.publisher.processor;

import lk.mrt.cse.wihidum.client.*;
import org.apache.axiom.om.*;
import org.apache.axiom.soap.*;
import org.apache.axis2.addressing.*;
import org.apache.log4j.*;

import javax.xml.namespace.*;

/**
 * Process publication messages
 */
public class PublicationProcessor
        implements Processor
{
    private static final Logger logger = Logger.getLogger (PublicationProcessor.class);

    /**
     * @see lk.mrt.cse.wihidum.publisher.processor.Processor#processMessage(org.apache.axiom.om.OMElement) 
     */
    public OMElement processMessage (OMElement requestElement)
    {
        try
        {
            logger.debug ("Processing publication message in Publisher service");

            OMElement brokerAddressElement = requestElement.getFirstChildWithName (new QName ("brokerAddress"));
            OMElement adminAddressElement = requestElement.getFirstChildWithName (new QName ("adminAddress"));
            OMElement publisherAddressElement = requestElement.getFirstChildWithName (new QName ("publisherAddress"));
            OMElement topicElement = requestElement.getFirstChildWithName (new QName ("topic"));
            OMElement publicationTextElement = requestElement.getFirstChildWithName (new QName ("publicationText"));

            PublicationClientBean clientBean = new PublicationClientBean ();

            clientBean.setBrokerAddress (new EndpointReference (brokerAddressElement.getText ()));
            clientBean.setAdminAddress (new EndpointReference (adminAddressElement.getText ()));
            clientBean.setPublisherAddress (new EndpointReference (publisherAddressElement.getText ()));
            clientBean.setTopic (topicElement.getText ());
            clientBean.setPublicationText (publicationTextElement.getText ());
            
            PublicationClient publicationClient = new PublicationClient (clientBean);

            String response = publicationClient.publish ();

            return createPublicationResponseElement (response);
        }
        catch (OMException ex)
        {
            logger.error ("Error in processing publication message", ex);
            return createPublicationFaultResponseElement ();
        }
    }

    /**
     * Create response for a valid publication
     *
     * @param response Response message
     * @return Response element
     */
    private OMElement createPublicationResponseElement (String response)
    {
        SOAPFactory factory = OMAbstractFactory.getSOAP11Factory ();

        OMElement responseElement = factory.createOMElement ("PublicationResponse", null);
        OMElement referenceParameters = factory.createOMElement ("ReferenceParameters", null);
        OMElement responseTextElement = factory.createOMElement ("Response", null);

        responseTextElement.setText (response);
        referenceParameters.addChild (responseTextElement);
        responseElement.addChild (referenceParameters);

        return responseElement;
    }

    /**
     * Create response for an invalid publication
     *
     * @return Response element
     */
    private OMElement createPublicationFaultResponseElement ()
    {
        return null;
    }
}
